import Vue from 'vue'
import VueRouter from 'vue-router'
// import login from '../components/login'
const login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/login.vue')
// import home from '../components/home'
const home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/home.vue')
// import welcome from '../components/welcome'
const welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/welcome.vue')
// import users from '../components/user/users'
const users = () => import(/* webpackChunkName:"user" */ '../components/user/users.vue')
// import powerList from '../components/power/powerList'
const powerList = () => import(/* webpackChunkName:"power" */ '../components/power/powerList.vue')
// import roleList from '../components/power/roleList'
const roleList = () => import(/* webpackChunkName:"power" */ '../components/power/roleList.vue')
// import goodsList from '../components/goods/goodsList'
const goodsList = () => import(/* webpackChunkName:"goods" */ '../components/goods/goodsList.vue')
// import categories from '../components/goods/categories'
const categories = () => import(/* webpackChunkName:"goods" */ '../components/goods/categories.vue')
// import params from '../components/goods/params'
const params = () => import(/* webpackChunkName:"goods" */ '../components/goods/params.vue')
// import addgoods from '../components/goods/addgoods'
const addgoods = () => import(/* webpackChunkName:"goods" */ '../components/goods/addgoods.vue')
// import orders from '../components/order/order'
const orders = () => import(/* webpackChunkName:"order" */ '../components/order/order.vue')
// import reports from '../components/reports/reports'
const reports = () => import(/* webpackChunkName:"reports" */ '../components/reports/reports.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    component: login
  },
  {
    path: "/home",
    component: home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: welcome },
      { path: '/users', component: users },
      { path: '/rights', component: powerList },
      { path: '/roles', component: roleList },
      { path: '/goods', component: goodsList },
      { path: '/categories', component: categories },
      { path: '/params', component: params },
      { path: '/goods/addgoods', component: addgoods },
      { path: '/orders', component: orders },
      { path: '/reports', component: reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 给每个请求体添加请求头信息
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
